// pages/home/index.js
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    home: [],
    offset: 0,
    limit: 10,
    toBePaid: [],
    toBeTravel: [],
    toBeEvaluated: [],
    allOrder: [{
      id: 0,
      name: '待支付',
      data: []
    }, {
      id: 1,
      name: '已支付',
      data: []
    }, {
      id: 2,
      name: '支付失败',
      data: []
    }, {
      id: 3,
      name: '待出行',
      data: []
    }, {
      id: 4,
      name: '已出行',
      data: []
    }, {
      id: 5,
      name: '待评价',
      data: []
    }, {
      id: 6,
      name: '已完成',
      data: []
    }, {
      id: 7,
      name: '退款中',
      data: []
    }, {
      id: 8,
      name: '退款完成',
      data: []
    }, {
      id: 100,
      name: '已取消',
      data: []
    }]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // this.init();
  },
  onShow: function (options) {
    this.init();
  },
  init() {
    var that = this
    this.data.allOrder.forEach((item, index) => {
      this.getItinerary(this.getOpt(item.id), function(data) {
        console.log(data)
        that.data.allOrder[index].data = data
        that.setData({
          allOrder: that.data.allOrder
        })
      })
    })

  },
  getItinerary(options, callback) {
    let defaultData = {
      user_id: util.isLogin().id,
      offset: this.data.offset,
      limit: this.data.limit,
    }
    util.http({
      url: 'api/miniwechat/center/itinerary',
      data: options || defaultData
    }).then((data) => {
      callback && callback(data)
    }).catch((err) => {
      wx.showToast({
        title: String(err),
        icon: 'none',
        duration: 2000
      })
    })
  },
  getOpt(status) {
    let options = {
      user_id: util.isLogin().id,
      offset: 0,
      limit: 5,
      status: status
    }
    return options
  },
  goMoreConent(e) {
    var status = e.currentTarget.dataset.status
    var title = e.currentTarget.dataset.title
    wx.navigateTo({
      url: '/pages/moreContent/index?status=' + status + '&title=' + title
    })
  },
  goOrderDetail(e) {
    let data = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/orderDetail/index?id=' + data.id
    })
  },
  goPaid(e) {
    console.log(e)
    let data = e.currentTarget.dataset;
    if (data.people > 0) {
      util.http({
        url: 'api/miniwechat/order/pay',
        method: 'POST',
        isPay: true,
        data: {
          out_trade_no: e.currentTarget.dataset.num,
          source: 2
        }
      }).then(res => {
        wx.requestPayment({
          timeStamp: String(res.timeStamp),
          nonceStr: String(res.nonceStr),
          package: String(res.package),
          signType: String(res.signType),
          paySign: String(res.paySign),
          success(resx) {
            wx.redirectTo({
              url: '/pages/paySuccess/index?state=1' + '&orderId=' + e.currentTarget.dataset.orderid
            })
          },
          fail(res) {
            console.log(res)
            wx.redirectTo({
              url: '/pages/paySuccess/index?state=0'
            })
          }
        })
      })
    } else {
      wx.setStorage({
        key: 'orderInfo',
        data: {
          user_id: util.isLogin().user_id,
          line_id: data.id,
          line_time_id: data.timeid,
          out_trade_no: data.num
        }
      })
      wx.navigateTo({
        url: '/pages/firmOrder/index?source=2',
      })
    }
  },
  cancelOrder(e) {
    let orderId = e.currentTarget.dataset.id
    let orderNo = e.currentTarget.dataset.num
    let that = this
    wx.showModal({
      title: '',
      content: '确认取消订单吗？',
      confirmColor: '#E50164',
      success(res) {
        if (res.confirm) {
          util.http({
            url: 'api/miniwechat/center/cancel_itinerary',
            method: 'POST',
            data: {
              user_id: util.isLogin().id,
              order_id: orderId,
              out_trade_no: orderNo
            }
          }).then(res => {
            that.init();
            wx.showToast({
              title: '取消成功',
              icon: 'none',
              duration: 2000
            })
          }).catch(err => {
            wx.showToast({
              title: '取消失败',
              icon: 'none',
              duration: 2000
            })
          })
        } else if (res.cancel) {
          console.log('取消')
        }
      }
    })
  },
  toRefund(e) {
    wx.navigateTo({
      url: '/pages/applyRefund/index?id=' + e.currentTarget.dataset.id,
    })
  },
  goRouteDetail(e) {
    wx.navigateTo({
      url: '/pages/routeDetail/index?id=' + e.currentTarget.dataset.id,
    })
  },
  goOrderDetail(e) {
    let data = e.currentTarget.dataset
    wx.navigateTo({
      url: '/pages/orderDetail/index?id=' + data.id
    })
  },

  refundDetail(e) {
    wx.navigateTo({
      url: '/pages/refund/index?id=' + e.currentTarget.dataset.id
    })
  },
  toComplete(e) {
    let orderId = e.currentTarget.dataset.id
    let time = e.currentTarget.dataset.time ? new Date(e.currentTarget.dataset.time) : '';
    let that = this;
    console.log(time)
    if (new Date() < time) {
      wx.showModal({
        title: '',
        content: '线路未出行完毕，点击“确认”后，款项将支付给商家，请确定',
        confirmColor: '#E50164',
        success(res) {
          if (res.confirm) {
            util.http({
              url: 'api/miniwechat/order/order_ful',
              method: 'POST',
              data: {
                user_id: util.isLogin().id,
                order_id: orderId,
              }
            }).then(res => {
              that.init();
              wx.showToast({
                title: '确认成功',
                icon: 'none',
                duration: 2000
              })
            }).catch(err => {
              wx.showToast({
                title: '确认失败',
                icon: 'none',
                duration: 2000
              })
            })
          } else if (res.cancel) {
            console.log('取消')
          }
        }
      })
      return;
    }
    wx.showModal({
      title: '',
      content: '确认已出行？',
      confirmColor: '#E50164',
      success(res) {
        if (res.confirm) {
          util.http({
            url: 'api/miniwechat/order/order_ful',
            method: 'POST',
            data: {
              user_id: util.isLogin().id,
              order_id: orderId,
            }
          }).then(res => {
            that.init();
            wx.showToast({
              title: '确认成功',
              icon: 'none',
              duration: 2000
            })
          }).catch(err => {
            wx.showToast({
              title: '确认失败',
              icon: 'none',
              duration: 2000
            })
          })
        } else if (res.cancel) {
          console.log('取消')
        }
      }
    })
  },
  toComment(e) {
    wx.navigateTo({
      url: '/pages/comment/index?lineId=' + e.currentTarget.dataset.lineid + '&orderId=' + e.currentTarget.dataset.orderid,
    })
  },
  toCommentDetail(e) {
    wx.navigateTo({
      url: '/pages/commDetail/index?lineId=' + e.currentTarget.dataset.lineid + '&orderId=' + e.currentTarget.dataset.orderid,
    })
  }
})